﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class PythonScriptArgs
    {
        public FileInfo MdFileInfo { get; set; }

        public string MdFilePath
        {
            get
            {
                return MdFileInfo.FullName;
            }
        }

        public string MdFileName
        {
            get
            {
                if (MdFileInfo == null || string.IsNullOrEmpty(MdFileInfo.Name))
                    return null;
                return MdFileInfo.Name;
            }
        }

        public string HtmlFileName
        {
            get
            {
                if (MdFileInfo == null || string.IsNullOrEmpty(MdFileInfo.Name))
                    return null;
                if (string.IsNullOrEmpty(MdFileInfo.Extension))
                    return null;
                return MdFileInfo.Name.Substring(0, MdFileInfo.Extension.Length) + ".html";
            }
        }

        public string HtmlFilePath
        {
            get
            {
                if (MdFileInfo == null || string.IsNullOrEmpty(MdFileInfo.FullName))
                    return null;
                if (string.IsNullOrEmpty(MdFileInfo.Extension))
                    return null;
                return MdFileInfo.FullName.Substring(0, MdFileInfo.Extension.Length) + ".html";
            }
        }

        public static List<string> GetAllFilePathsOfWorkspace(string mdOrHtml)
        {
            if (Globals.MainWindow.tvWorkDirectory.Items.Count <= 0) return null;

            var list = new List<string>();
            GetSubFilePaths(Globals.MainWindow.tvWorkDirectory.Items[0] as WorkspaceTreeViewItem, ref list, mdOrHtml);
            return list;
        }

        private static void GetSubFilePaths(WorkspaceTreeViewItem wtvi, ref List<string> list, string mdOrHtml)
        {
            if (wtvi == null) return;

            switch (wtvi.ItemType)
            {
                case WorkspaceTreeViewItem.Type.File:
                    {
                        if (string.IsNullOrWhiteSpace(wtvi.FullPath) == false)
                        {
                            switch (mdOrHtml.ToLower())
                            {
                                case "html":
                                    {
                                        list.Add(wtvi.HtmlFullPath);
                                        break;
                                    }
                                default:
                                    {
                                        list.Add(wtvi.FullPath);
                                        break;
                                    }
                            }
                        }
                        break;
                    }
                case WorkspaceTreeViewItem.Type.Folder:
                    {
                        if (string.IsNullOrWhiteSpace(wtvi.MetaFilePath) == false)
                        {
                            switch (mdOrHtml.ToLower())
                            {
                                case "html":
                                    {
                                        list.Add(wtvi.MetaHtmlFilePath);
                                        break;
                                    }
                                default:
                                    {
                                        list.Add(wtvi.MetaFilePath);
                                        break;
                                    }
                            }
                        }

                        if (wtvi.Items.Count > 0)
                        {
                            foreach (var subItem in wtvi.Items)
                            {
                                var subWtiv = subItem as WorkspaceTreeViewItem;
                                GetSubFilePaths(subWtiv, ref list, mdOrHtml);
                            }
                        }
                        break;
                    }
            }
        }

        public MarkdownEditorBase Ate { get { return Globals.MainWindow?.ActiveTextEditor; } }

        public MarkdownEditor Ae { get { return Globals.MainWindow?.ActivedEditor; } }
    }
}
